local thbattle__huiyin_jueyi = fk.CreateSkill {

  name = "thbattle__huiyin_jueyi",

  tags = { Skill.Compulsory, },

}



thbattle__huiyin_jueyi:addEffect(fk.DamageInflicted, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if #table.filter(player.room.alive_players, function(p)
          return p:hasSkill(thbattle__huiyin_jueyi.name) and p:getMark("thbattle__huiyin_jueyi") ~= 0
        end) < 2 then
      return
    end
    local target_p=table.find(room:getOtherPlayers(player),function (p)
      return player:getMark("thbattle__huiyin_jueyi")==p.id
    end)
    if target_p==nil then
      return
    end
    if event == fk.DamageInflicted and target_p.hp > player.hp and target == player then
      if player:hasSkill(thbattle__huiyin_jueyi.name) and player:getMark("thbattle__huiyin_jueyi") ~= 0 then
        local target_p = player.room:getPlayerById(player:getMark("thbattle__huiyin_jueyi"))
        room:setPlayerMark(player, "@thbattle__huiyin_jueyi", Fk:translate(target_p.general))
        room:setPlayerMark(target_p, "@thbattle__huiyin_jueyi", Fk:translate(player.general))
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target_p = player.room:getPlayerById(player:getMark("thbattle__huiyin_jueyi"))
    room:notifySkillInvoked(player, thbattle__huiyin_jueyi.name, "support")
    player:broadcastSkillInvoke(thbattle__huiyin_jueyi.name, 1)
    local damageEvent = data
    damageEvent.to = target_p
    room:damage(damageEvent)
    data:preventDamage()
  end,
})
thbattle__huiyin_jueyi:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if #table.filter(player.room.alive_players, function(p)
          return p:hasSkill(thbattle__huiyin_jueyi.name) and p:getMark("thbattle__huiyin_jueyi") ~= 0
        end) < 2 then
      return
    end
    if player:hasSkill(thbattle__huiyin_jueyi.name) and player:getMark("thbattle__huiyin_jueyi") ~= 0 then
      local target_p = player.room:getPlayerById(player:getMark("thbattle__huiyin_jueyi"))
      if event == fk.AfterCardsMove and player.phase ~= Player.NotActive then
        for _, move in ipairs(data) do
          if move.to == target_p and move.moveReason == fk.ReasonDraw and #move.moveInfo > 0 then
            room:setPlayerMark(player, "@thbattle__huiyin_jueyi", Fk:translate(target_p.general))
            room:setPlayerMark(target_p, "@thbattle__huiyin_jueyi", Fk:translate(player.general))
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target_p = player.room:getPlayerById(player:getMark("thbattle__huiyin_jueyi"))
    room:notifySkillInvoked(player, thbattle__huiyin_jueyi.name, "support")
    player:broadcastSkillInvoke(thbattle__huiyin_jueyi.name, 3)
    for _, move in ipairs(data) do
      if move.to == target_p and move.moveReason == fk.ReasonDraw then
        player:drawCards(#move.moveInfo, thbattle__huiyin_jueyi.name)
      end
    end
  end,
})
thbattle__huiyin_jueyi:addEffect(fk.HpRecover, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if #table.filter(player.room.alive_players, function(p)
          return p:hasSkill(thbattle__huiyin_jueyi.name) and p:getMark("thbattle__huiyin_jueyi") ~= 0
        end) < 2 then
      return
    end
    if player:hasSkill(thbattle__huiyin_jueyi.name) and player:getMark("thbattle__huiyin_jueyi") ~= 0 then
      local target_p = player.room:getPlayerById(player:getMark("thbattle__huiyin_jueyi"))
      if event == fk.HpRecover and data.who == target_p and data.skillName ~= thbattle__huiyin_jueyi.name then
        room:setPlayerMark(player, "@thbattle__huiyin_jueyi", Fk:translate(target_p.general))
        room:setPlayerMark(target_p, "@thbattle__huiyin_jueyi", Fk:translate(player.general))
        return player.phase ~= Player.NotActive and player:isWounded()
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target_p = player.room:getPlayerById(player:getMark("thbattle__huiyin_jueyi"))
    room:notifySkillInvoked(player, thbattle__huiyin_jueyi.name, "support")
    player:broadcastSkillInvoke(thbattle__huiyin_jueyi.name, 2)
    room:recover({
      who = player,
      num = data.num,
      recoverBy = player,
      skillName = thbattle__huiyin_jueyi.name
    })
  end,
})

return thbattle__huiyin_jueyi
